package com.xiaozhen.alarm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaozhen.alarm.domain.AlarmConfig;
import com.xiaozhen.alarm.domain.queryParams.AlarmConfigParam;
import com.xiaozhen.alarm.service.AlarmConfigService;
import com.xiaozhen.common.core.domain.AjaxResult;
import com.xiaozhen.security.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/10/25 9:18
 * @description:
 */
@RestController
@Api(tags = "告警配置的接口")
@RequestMapping("/alarmConfig")
public class AlarmConfigController extends BaseController {

    @Autowired
    private AlarmConfigService alarmConfigService;


    @ApiOperation("获取所有配置列表")
    @GetMapping("/list")
    public AjaxResult list(AlarmConfigParam param) {
        Page<AlarmConfig> page = startPage();
        Page<AlarmConfig> list = alarmConfigService.selectAlarmConfigList(page, param);
        return getDataTable(list);
    }


    @ApiOperation("新增配置")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody AlarmConfig alarmConfig) {
        alarmConfig.setCreateBy(getCurrentUserId());
        alarmConfig.setCreateTime(LocalDateTime.now());
        return toAjax(alarmConfigService.insertAlarmConfig(alarmConfig));
    }

    @ApiOperation("修改配置")
    @PutMapping("/update")
    public AjaxResult edit(@Validated @RequestBody AlarmConfig alarmConfig) {
        alarmConfig.setUpdateBy(getCurrentUserId());
        alarmConfig.setUpdateTime(LocalDateTime.now());
        return toAjax(alarmConfigService.updateAlarmConfig(alarmConfig));
    }

    @ApiOperation("删除配置")
    @DeleteMapping("/{configId}")
    public AjaxResult remove(@PathVariable Long configId) {
        return toAjax(alarmConfigService.deleteAlarmConfigById(configId));
    }
}
